package com.qiezi.project.activity;


import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiezi.R;
import com.qiezi.base.BaseMvpActivity;
import com.qiezi.mvp.CreatePresenter;
import com.qiezi.project.adapter.CategoryAdapter;
import com.qiezi.project.adapter.RecommendAdapter;
import com.qiezi.project.adapter.WatchHistoryAdapter;
import com.qiezi.project.contract.WatchHistoryContract;
import com.qiezi.util.FontUtils;

import java.util.ArrayList;

import butterknife.BindView;


@CreatePresenter(presenter = WatchHistoryContract.Presenter.class)
public class WatchHistoryActivity extends BaseMvpActivity<WatchHistoryContract.Presenter> implements WatchHistoryContract.View,View.OnClickListener,SwipeRefreshLayout.OnRefreshListener {

    static final public int REQUEST_CODE = 1;


    @BindView(R.id.tv_back)
    TextView tvBack;

    @BindView(R.id.tv_topBar_title)
    TextView tvTopBarTitle;

    @BindView(R.id.recy)
    RecyclerView recyclerView;


    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_recycler;
    }

    private void initTopbar(){
        Typeface iconfont = Typeface.createFromAsset(getAssets(), "fonts/iconfont.ttf");
        tvBack.setTypeface(iconfont);
        tvTopBarTitle.setText("观看历史");
    }


    private WatchHistoryAdapter adapter;
    @Override
    protected void initData(Bundle saveBundle) {
        initTopbar();
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        adapter = new WatchHistoryAdapter();
        adapter.setEmptyView(getEmptyView(R.drawable.ic_history));
        recyclerView.setAdapter(adapter);

        refresh.setColorSchemeResources(R.color.colorRed);
        refresh.setOnRefreshListener(this);
//        refresh.setRefreshing(true);
//        onRefresh();
    }


    private View getEmptyView(int imageID){
        RelativeLayout layout = new RelativeLayout(this);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,RecyclerView.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        params1.addRule(RelativeLayout.CENTER_VERTICAL);
        params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
        linearLayout.setLayoutParams(params1);
        layout.addView(linearLayout);

        ImageView iv = new ImageView(this);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,dip2px(120));
        iv.setLayoutParams(params2);
        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        iv.setImageDrawable(ContextCompat.getDrawable(this,imageID));
        linearLayout.addView(iv);


        TextView tvMore = new TextView(this);
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,dip2px(35));
        params3.topMargin = FontUtils.dip2px(this,10);
        params3.gravity = Gravity.CENTER;
        tvMore.setPadding(dip2px(10),0,dip2px(10),0);
        tvMore.setLayoutParams(params3);
        tvMore.setText("查看更多影片");
        tvMore.setTextColor(ContextCompat.getColor(this,R.color.colorWhite));
        tvMore.setTextSize(14);
        tvMore.setGravity(Gravity.CENTER);
        tvMore.setBackground(ContextCompat.getDrawable(this,R.drawable.bg_btn_save));
        linearLayout.addView(tvMore);
        tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(RESULT_OK);
            }
        });
        return layout;
    }


    @Override
    protected void initListener() {
        tvBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
        }
    }

    @Override
    public void resultData(ArrayList data) {
        adapter.setDatas(data);
    }

    @Override
    public void onRefresh() {

    }

}
